//
//  WalletWithdrawViewController.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/6.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "WalletWithdrawViewController.h"
#import "WalletWithDrawMoneyViewController.h"


#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "SVProgressHUD.h"
@interface WalletWithdrawViewController ()

@property (nonatomic, copy)NSString *smsCode;
@property (nonatomic, strong)UIButton *codeBtn;
@property (nonatomic, strong)UITextField *codeText;


@end

@implementation WalletWithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    [self.titleBtn setTitle:@"提现" forState:UIControlStateNormal];

    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, self.navview.frame.size.height+15, SCREENW-30, 30)];
    label.text = @"为了您的账户安全，我们将向您绑定的手机号发送验证码。";
    label.numberOfLines = 0;
    label.textColor = bgmsmallColor;
    label.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:label];
    [label sizeToFit];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, label.frame.origin.y+label.frame.size.height+20, SCREENW, 0.3)];
    line.backgroundColor = [UIColor grayColor];
    line.alpha = 0.1;
    [self.view addSubview:line];
    
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(15, line.frame.origin.y+30, 100, 30)];
    lable1.text = @"手机号";
    lable1.textColor = bgmsmallColor;
    [self.view addSubview:lable1];
    
    UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(100, line.frame.origin.y+30, 120, 30)];
    NSString *iphone = [BGMUser shareInstance].phone;;
    lable2.text = [NSString stringWithFormat:@"%@****%@",[iphone substringWithRange:NSMakeRange(0, 3)],[iphone substringWithRange:NSMakeRange(7, 4)]];
    lable2.textColor = bgmsmallColor;
    [self.view addSubview:lable2];
    
    self.codeBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-140, lable2.frame.origin.y-5, 110,40)];
    self.codeBtn.backgroundColor = bgmyelloeColor;
    [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
    self.codeBtn.layer.cornerRadius = 8;
    self.codeBtn.layer.masksToBounds = YES;
    self.codeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.codeBtn addTarget:self action:@selector(clickSmscode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.codeBtn];
    
    self.codeText = [[UITextField alloc]initWithFrame:CGRectMake(15, self.codeBtn.frame.origin.y+self.codeBtn.frame.size.height +50, SCREENW-30, 30)];
    self.codeText.placeholder = @"输入验证码";
    self.codeText.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.codeText];
    
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(15, self.codeText.frame.origin.y+self.codeText.frame.size.height, SCREENW-30, 1)];
    line1.backgroundColor = [UIColor grayColor];
    line1.alpha = 0.3;
    [self.view addSubview:line1];
    
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(30, line1.frame.origin.y+line1.frame.size.height+40, SCREENW-60,45)];
    btn1.backgroundColor = bgmyelloeColor;
    [btn1 setTitle:@"提交" forState:UIControlStateNormal];
    [btn1 setTitleColor:bgmmainColor forState:UIControlStateNormal];
    btn1.layer.cornerRadius = 8;
    btn1.layer.masksToBounds = YES;
    btn1.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(clickSumbit) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickSmscode{
    [self startTimeDown];
    NSDictionary *params = @{@"phoneNum":[BGMUser shareInstance].phone};
    [BGMHTTP requestWithMethodName:@"login/getsmsCode" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            self.smsCode = [NSString stringWithFormat:@"%@",[result objectForKey:@"data"]];
            self.codeText.text = self.smsCode;
        }
    }];
    
}

- (void)startTimeDown{
    __block int timeout = 59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if (timeout <= 0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:@"再次获取" forState:UIControlStateNormal];
                self.codeBtn.backgroundColor = bgmyelloeColor;
                self.codeBtn.enabled = YES;
            });
        } else {
            NSString *strTime = [NSString stringWithFormat:@"%ds", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:strTime forState:UIControlStateNormal];
                self.codeBtn.backgroundColor = bgmColor(244, 244, 244);
                self.codeBtn.enabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (void)clickSumbit{
    if (self.smsCode.length>0) {
        if ([self.codeText.text isEqualToString:self.smsCode]) {
            WalletWithDrawMoneyViewController *vc = [[WalletWithDrawMoneyViewController alloc]init];
            vc.smsCode = self.smsCode;
            vc.money = self.money;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:@"请输入正确的验证码"];
        }
    }else{
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
    }
}



@end
